from odoo import api, fields, models, _
from odoo.exceptions import ValidationError

class MrpProduction(models.Model):
    _inherit = 'mrp.production'

    is_lock = fields.Boolean(string='Lock or Unlock', default=False, copy=False)
    lock_reason = fields.Html(string='Lock Reason')
    # 禁止出货
    s_disable_delivery = fields.Boolean(string='禁止过数', tracking=True)

    def button_mark_done(self):
        for rec in self:
            if rec.is_lock:
                raise ValidationError('Sale order is lock, please unlock')
        return super().button_mark_done()

    def button_plan(self):
        for rec in self:
            if rec.is_lock:
                raise ValidationError('Sale order is lock, please unlock')
        return super().button_plan()

    def button_unplan(self):
        for rec in self:
            if rec.is_lock:
                raise ValidationError('Sale order is lock, please unlock')
        return super().button_unplan()

    def button_scrap(self):
        for rec in self:
            if rec.is_lock:
                raise ValidationError('Sale order is lock, please unlock')
        return super().button_scrap()

    def action_cancel(self):
        for rec in self:
            if rec.is_lock:
                raise ValidationError('Sale order is lock, please unlock')
        return super().action_cancel()

    def action_rework(self):
        for rec in self:
            if rec.is_lock:
                raise ValidationError('Sale order is lock, please unlock')
        return super().action_rework()

    def action_remark(self):
        for rec in self:
            if rec.is_lock:
                raise ValidationError('Sale order is lock, please unlock')
        return super().action_remark()

    def _unlock_order(self, sale_order):
        # 解锁销售单、发货的、工单、报废、过数等
        mrp_prods = set()
        # 解锁销售单
        sale_order.is_lock = False
        order_lines = sale_order.order_line
        picking_ids = sale_order.picking_ids
        sale_order.option_mrp_production_ids.filtered(lambda x: x.is_lock).is_lock = False
        for line in order_lines:
            for mrp in line.s_mrp_production_ids:
                if not mrp_prods:
                    mrp_prods = mrp
                mrp_prods |= self._get_parent_mrp(mrp)
                mrp_prods |= self._get_child_mrp(mrp)
                picking_ids |= mrp.picking_ids

        # 解锁工单
        for mrp in mrp_prods:
            mrp.filtered(lambda x: x.state != 'done').is_lock = False
            # mrp.is_lock = True
        # 解锁picking(出库和生产)
        for picking in picking_ids:
            picking.filtered(lambda x: x.state != 'done').is_lock = False
            # picking.is_lock = True

    def _get_parent_mrp(self, mrp):
        parent_ids = mrp._get_sources()
        # 欠单
        mrp_backorders = mrp.procurement_group_id.mrp_production_ids
        return parent_ids | mrp_backorders | mrp

    def _get_child_mrp(self, mrp):
        child_ids = mrp._get_children()
        # 欠单
        mrp_backorders = mrp.procurement_group_id.mrp_production_ids
        return child_ids | mrp_backorders
